package com.zmn.mcc.services.impl.dept;

import com.zmn.common.utils.date.DateUtil;
import com.zmn.mcc.model.dto.dpermit.DpermitCountDO;
import com.zmn.mcc.common.dto.permit.DpermitLitDO;
import com.zmn.mcc.model.entity.dept.McDeptDpermit;
import com.zmn.mcc.model.entity.dept.McDeptDpermitQuery;
import com.zmn.mcc.persistence.interfaces.dept.DeptDpermitDao;
import com.zmn.mcc.services.interfaces.dept.DeptDpermitService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author heguanghua
 * @since 2020/04/14 18:11
 */
@Service
public class DeptDpermitServiceImpl implements DeptDpermitService {

    @Resource
    private DeptDpermitDao deptDpermitDao;


    @Override
    public McDeptDpermit findByKey(Integer deptId, Integer dataType, Integer dataLevel, Integer dataId) {
        McDeptDpermit deptDpermit = new McDeptDpermit();
        deptDpermit.setDeptId(deptId);
        deptDpermit.setDataType(dataType);
        deptDpermit.setDataLevel(dataLevel);
        deptDpermit.setDataId(dataId);
        return this.findByKey(deptDpermit);
    }
    @Override
    public McDeptDpermit findByKey(McDeptDpermit deptDpermit) {
        return deptDpermitDao.findByKey(deptDpermit);
    }

    @Override
    public McDeptDpermit findByQuery(McDeptDpermitQuery query) {
        return deptDpermitDao.findByQuery(query);
    }

    @Override
    public List<McDeptDpermit> listPageByQuery(McDeptDpermitQuery query) {
        Integer count = this.countByQuery(query);
        query.setTotalCount(count);
        return count == 0 ? new ArrayList<>(0) : deptDpermitDao.listPageByQuery(query);
    }

    @Override
    public List<McDeptDpermit> listByDeptId(Integer deptId) {
        McDeptDpermitQuery query = new McDeptDpermitQuery();
        query.setDeptId(deptId);
        return this.listByQuery(query);
    }
    @Override
    public List<McDeptDpermit> listByDeptIdAndDataType(Integer deptId, Integer dataType) {
        McDeptDpermitQuery query = new McDeptDpermitQuery();
        query.setDeptId(deptId);
        query.setDataType(dataType);
        return this.listByQuery(query);
    }
    @Override
    public List<McDeptDpermit> listByDeptIdAndDataAttr(Integer deptId, Integer dataType, Integer dataLevel) {
        McDeptDpermitQuery query = new McDeptDpermitQuery();
        query.setDeptId(deptId);
        query.setDataType(dataType);
        query.setDataLevel(dataLevel);
        return this.listByQuery(query);
    }
    @Override
    public List<McDeptDpermit> listByDeptIdAndDataAttr(Integer deptId, Integer dataType, List<Integer> dataLevels) {
        McDeptDpermitQuery query = new McDeptDpermitQuery();
        query.setDeptId(deptId);
        query.setDataType(dataType);
        query.setDataLevels(dataLevels);
        return this.listByQuery(query);
    }

    @Override
    public List<McDeptDpermit> listByQuery(McDeptDpermitQuery query) {
        return deptDpermitDao.listByQuery(query);
    }

    @Override
    public List<Integer> listDataIdByDeptIdAndDataType(Integer deptId, Integer dataType) {
        McDeptDpermitQuery query = new McDeptDpermitQuery();
        query.setDeptId(deptId);
        query.setDataType(dataType);
        return this.listDataIdByQuery(query);
    }
    @Override
    public List<Integer> listDataIdByDeptIdAndDataAttr(Integer deptId, Integer dataType, Integer dataLevel) {
        McDeptDpermitQuery query = new McDeptDpermitQuery();
        query.setDeptId(deptId);
        query.setDataType(dataType);
        query.setDataLevel(dataLevel);
        return this.listDataIdByQuery(query);
    }
    @Override
    public List<Integer> listDataIdByDeptIdAndDataAttr(Integer deptId, Integer dataType, List<Integer> dataLevels) {
        McDeptDpermitQuery query = new McDeptDpermitQuery();
        query.setDeptId(deptId);
        query.setDataType(dataType);
        query.setDataLevels(dataLevels);
        return this.listDataIdByQuery(query);
    }
    @Override
    public List<Integer> listDataIdByQuery(McDeptDpermitQuery query) {
        return deptDpermitDao.listDataIdByQuery(query);
    }

    @Override
    public List<DpermitLitDO> listDpermitLitByStaffIdAndDataType(Integer deptId, Integer dataType) {
        McDeptDpermitQuery query = new McDeptDpermitQuery();
        query.setDeptId(deptId);
        query.setDataType(dataType);
        return this.listDpermitLitByQuery(query);
    }
    @Override
    public List<DpermitLitDO> listDpermitLitByQuery(McDeptDpermitQuery query) {
        return deptDpermitDao.listDpermitLitByQuery(query);
    }

    @Override
    public Integer countByQuery(McDeptDpermitQuery query) {
        return deptDpermitDao.countByQuery(query);
    }

    @Override
    public List<DpermitCountDO> countByQueryGroupByParentId(McDeptDpermitQuery query) {
        return deptDpermitDao.countByQueryGroupByParentId(query);
    }



    @Override
    public Integer updateByKey(McDeptDpermit deptDpermit) {
        return deptDpermitDao.updateByKey(deptDpermit);
    }

    @Override
    public Integer updateSubAllByQuery(McDeptDpermitQuery query) {
        return deptDpermitDao.updateSubAllByQuery(query);
    }



    @Override
    public Integer insert(McDeptDpermit deptDpermit) {
        if(deptDpermit.getCreateTime() == null) {
            deptDpermit.setCreateTime(DateUtil.getNow());
        }
        return deptDpermitDao.insert(deptDpermit);
    }

    @Override
    public Integer insertByBatch(List<McDeptDpermit> deptDpermits) {
        return deptDpermitDao.insertByBatch(deptDpermits);
    }


    @Override
    public Integer deleteByKey(Integer deptId, Integer dataType, Integer dataLevel, Integer dataId) {
        McDeptDpermit deptDpermit = new McDeptDpermit();
        deptDpermit.setDeptId(deptId);
        deptDpermit.setDataType(dataType);
        deptDpermit.setDataLevel(dataLevel);
        deptDpermit.setDataId(dataId);
        return this.deleteByKey(deptDpermit);
    }
    @Override
    public Integer deleteByKey(McDeptDpermit deptDpermit) {
        return deptDpermitDao.deleteByKey(deptDpermit);
    }

    @Override
    public Integer deleteByDeptId(Integer deptId) {
        McDeptDpermitQuery query = new McDeptDpermitQuery();
        query.setDeptId(deptId);
        return this.deleteByQuery(query);
    }
    @Override
    public Integer deleteByDeptIdAndDataType(Integer deptId, Integer dataType) {
        McDeptDpermitQuery query = new McDeptDpermitQuery();
        query.setDeptId(deptId);
        query.setDataType(dataType);
        return this.deleteByQuery(query);
    }
    @Override
    public Integer deleteByDeptIdAndDataAttr(Integer deptId, Integer dataType, List<Integer> dataLevels) {
        McDeptDpermitQuery query = new McDeptDpermitQuery();
        query.setDeptId(deptId);
        query.setDataType(dataType);
        query.setDataLevels(dataLevels);
        return this.deleteByQuery(query);
    }

    @Override
    public Integer deleteByQuery(McDeptDpermitQuery query) {
        return deptDpermitDao.deleteByQuery(query);
    }
}
